package gui ;

import java.sql.SQLException;

import com.trolltech.qt.gui.* ;
import interne.Client ;
import gestionBDD.requetesClient ;

public class fenetreInscription extends QWidget {

    private QLineEdit loginLineEdit;
    private QLineEdit passwordLineEdit ;
    private QLineEdit nomLineEdit ;
    private QLineEdit prenomLineEdit ;
    private QLineEdit adresseLineEdit ;
	
    /**
     * Constructeur de la fenêtre Inscription
     */
    public fenetreInscription() {
    	
        /** Login's GroupBox' **/
        QGroupBox loginGroup = new QGroupBox(tr("Identifiants :"));
 
        /* Login Line Edit with Label */
        QLabel loginLabel = new QLabel(tr("E-Mail :"));
        loginLineEdit = new QLineEdit();
        loginLineEdit.setFocus();
        
        /* Password LineEdit with Label */
        QLabel passwordLabel = new QLabel(tr("Mot de passe :"));
        passwordLineEdit = new QLineEdit();
        passwordLineEdit.setEchoMode(QLineEdit.EchoMode.Password);
        
        /* Login's Layout */
        QGridLayout loginLayout = new QGridLayout();
        loginLayout.addWidget(loginLabel, 0, 0);
        loginLayout.addWidget(loginLineEdit, 0, 1);
        loginLayout.addWidget(passwordLabel, 1, 0);
        loginLayout.addWidget(passwordLineEdit, 1, 1);
        loginGroup.setLayout(loginLayout);
        
        /** Coordonnées' GroupBox' **/
        QGroupBox coordonneesGroup = new QGroupBox(tr("Coordonnées :"));
 
        /* Nom Line Edit with Label */
        QLabel nomLabel = new QLabel(tr("Nom :"));
        nomLineEdit = new QLineEdit();
        nomLineEdit.setFocus();
        
        /* Prénom LineEdit with Label */
        QLabel prenomLabel = new QLabel(tr("Prénom :"));
        prenomLineEdit = new QLineEdit();
        
        /* Adresse LineEdit with Label */
        QLabel adresseLabel = new QLabel(tr("Adresse :"));
        adresseLineEdit = new QLineEdit();
        
        /* Login's Layout */
        QGridLayout coordonneesLayout = new QGridLayout();
        coordonneesLayout.addWidget(nomLabel, 0, 0);
        coordonneesLayout.addWidget(nomLineEdit, 0, 1);
        coordonneesLayout.addWidget(prenomLabel, 1, 0);
        coordonneesLayout.addWidget(prenomLineEdit, 1, 1);
        coordonneesLayout.addWidget(adresseLabel, 2, 0);
        coordonneesLayout.addWidget(adresseLineEdit, 2, 1);
        coordonneesGroup.setLayout(coordonneesLayout);

        /* Dialog Button Box */
        QDialogButtonBox boutons = new QDialogButtonBox();
                boutons.addButton(QDialogButtonBox.StandardButton.Ok);
  		      	boutons.clicked.connect(this, "inscrire()");        

        // Dialog Layout
        QGridLayout layout = new QGridLayout();
        layout.addWidget(loginGroup, 0, 0);
        layout.addWidget(coordonneesGroup, 1, 0);
        layout.addWidget(boutons, 2, 0);
        setWindowTitle(tr("Inscription"));
        setLayout(layout);
    
    }
    
    /**
     * Validation de l'inscription
     * Infos : Ajoute une entrée dans la table Client
     * @throws SQLException
     */
    public void inscrire() throws SQLException {
    	try {
    		Client c = new Client(loginLineEdit.text(), passwordLineEdit.text(), 
    				nomLineEdit.text(), prenomLineEdit.text(), adresseLineEdit.text()) ;
    		requetesClient.ajouterClient(c) ;
       		this.hide() ;
    	}
    	catch (SQLException e) {
    		System.out.println(e) ;
    	}
    }
}
